﻿using PetaPoco;
using Shop.Order.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shop.Order.Repositories
{
    /// <summary>
    /// 订单管理数据访问层
    /// </summary>
    public class OrderAccess
    {
        private Database DB = new Database("ConnString");
        #region 用户产生订单
        /// <summary>
        /// 生成子订单
        /// </summary>
        /// <param name="odinfo"></param>
        /// <returns></returns>
        public object CreateOrderDetail(OrderDetail odinfo)
        {
            return DB.Insert("Shop_OrderDetail", "ONID", odinfo);
        }
        /// <summary>
        /// 生成订单
        /// </summary>
        /// <param name="oinfo"></param>
        /// <returns></returns>
        public object CreateOrder(Models.Order oinfo)
        {
            return DB.Insert("Shop_Order", "OrderNumber", false, oinfo);
        }
        /// <summary>
        /// 获取订单号
        /// </summary>
        /// <returns></returns>
        public string GetOrderNumber()
        {
            Sql sql = Sql.Builder.Append("exec dpPMT_SGetMaintainSeq 'shop'");
            //需要加上这么一句，是否自动添加select子句
            DB.EnableAutoSelect = false;
            return DB.SingleOrDefault<string>("exec dpPMT_SGetMaintainSeq 'shop'");
        }
        #endregion

        #region 用户订单管理
        /// <summary>
        /// 根据用户ID获取用户订单信息
        /// </summary>
        /// <param name="Page">第几页</param>
        /// <param name="PageNum">每页多少行</param>
        /// <param name="UID"></param>
        /// <returns></returns>
        public Page<Models.AnyOrder> GetUserOInfo(long Page, long PageNum,int UID)
        {
            Sql sql = Sql.Builder.Append("select Shop_Order.* from Shop_Order  where Shop_Order.UID = @0 order by Shop_Order.CreateDate desc", UID);
            return DB.Page<Models.AnyOrder>(Page, PageNum, sql);
        }
        /// <summary>
        /// 根据订单号获取订单详情
        /// </summary>
        /// <param name="OrderNumber"></param>
        /// <returns></returns>
        public IList<Models.AnyOrderDetail> GetUserODInfo(string OrderNumber)
        {
            Sql sql = Sql.Builder.Append("select Shop_OrderDetail.* ,Shop_Wares.WName,Shop_Wares.Url from Shop_OrderDetail join Shop_Wares on Shop_OrderDetail.WID = Shop_Wares.WID where Shop_OrderDetail.OrderNumber = @0", OrderNumber);
            return DB.Fetch<Models.AnyOrderDetail>(sql);
        }
        #endregion

        #region 管理员后台管理
        /// <summary>
        /// 后台获取订单信息
        /// </summary>
        /// <param name="Page">第几页</param>
        /// <param name="PageNum">每页多少条</param>
        /// <param name="IfPay">是否支付</param>
        /// <param name="Status">订单状态</param>
        /// <param name="Strart">开始时间</param>
        /// <param name="End">结束时间</param>
        /// <param name="SearchType">搜索类型</param>
        /// <param name="Search">搜索内容</param>
        /// <param name="order">排序方式</param>
        /// <returns></returns>
        public Page<Models.Order> GetOInfo(long Page, long PageNum, int IfPay, int Status, string Strart, string End,int SearchType,string Search,int order)
        {
            string strsql = string.Format("select * from Shop_Order where ");
            string WhatSearch = "";
            switch (SearchType)
            {
                case 1:
                    WhatSearch = "OrderNumber";
                    break;
                default:
                    break;
            }
            strsql += string.Format(" {0} like '%{1}%'", WhatSearch, Search);

            //这里把9看为全部
            if (Status != 9)
            {
                strsql += string.Format("and Status = {0} ", Status);
            }
            if (IfPay != 2)
            {
                strsql += string.Format("and IfPay = {0} ", IfPay);
            }
            if (!string.IsNullOrEmpty(Strart))
            {
                strsql += string.Format(" and CreateDate > '{0}' and CreateDate < '{1}' ",Strart,End);
            }
            //这里把0为desc为正序1为倒序
            if (order == 0)
            {
                strsql += string.Format("order by CreateDate desc");
            }

            Sql sql = Sql.Builder.Append(strsql);
            return DB.Page<Models.Order>(Page, PageNum, sql);
        }
        /// <summary>
        /// 根据订单号获取订单信息
        /// </summary>
        /// <param name="OrderNumber">订单号</param>
        /// <returns></returns>
        public Models.Order GetOInfoByOrderNumber(string OrderNumber)
        {
            Sql sql = Sql.Builder.Append("select * from Shop_Order where OrderNumber = @0", OrderNumber);
            return DB.FirstOrDefault<Models.Order>(sql);
        }
        /// <summary>
        /// 将订单状态改为已发货
        /// </summary>
        /// <param name="OrderNumber"></param>
        /// <returns></returns>
        public int Delivery(string OrderNumber,string CourierNumber,string CCode)
        {
            Sql sql = Sql.Builder.Append("update Shop_Order set Status = 1,CourierNumber = @0,CCode = @1 where OrderNumber = @2", CourierNumber, CCode, OrderNumber);
            return DB.Execute(sql);
        }
        #endregion
    }
}
